﻿using FineUI;
using SysManage.BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;
using ZHDJ.Core;
using ZHDJ.Data.Models.SeekHelp;
using ZHDJ.Data.Models.Donation;
using ZHDJ.PersonalCenter.BLL;
using ZHDJ.WebPage;

namespace ZHDJ.PersonalCenter.Module.DonationAndSeekHelp
{
    public partial class DonAndSeekList : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "DonAndSeekView";
            }
        }
        #endregion

        #region 属性
        //求助
        private DataSet SeekHelpDataSource
        {
            get
            {
                return PartySeekHelp.GetListData(gvSeekHelp.PageIndex, gvSeekHelp.PageSize, CurrentUser.LoginID, txtName.Text.Trim(), ddlType.SelectedValue, ddlState.SelectedValue);
            }
        }

        //捐助
        private DataSet DonationDataSource
        {
            get
            {
                return PartyDonation.GetListData(gvDonation.PageIndex, gvDonation.PageSize, CurrentUser.LoginID, txtName.Text.Trim(), ddlType.SelectedValue, ddlState.SelectedValue, ddlDName.SelectedValue, ddlDFrom.SelectedValue, ddlPurpose.SelectedValue);
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindControl();
                LoadData();
            }
        }

        #region 数据加载
        private void LoadData()
        {
            // 权限检查
            CheckPowerWithButton("DonAndSeekMate", btnMate);

            // 每页记录数
            gvSeekHelp.PageSize = ConfigHelper.PageSize;
            gvDonation.PageSize = ConfigHelper.PageSize;
            BindSeekHelp();
            BindDonation();
        }

        private void BindControl()
        {
            ControlBusiness.ControlBindDataDictionary(ddlType, SysDataDictionary.DonationType, true);
            ControlBusiness.ControlBindDataDictionary(ddlState, SysDataDictionary.DonationState, true);
            ControlBusiness.ControlBindDataDictionary(ddlDName, SysDataDictionary.DonationDName, true);
            ControlBusiness.ControlBindDataDictionary(ddlDFrom, SysDataDictionary.DonationDFrom, true);
            ControlBusiness.ControlBindDataDictionary(ddlPurpose, SysDataDictionary.DonationPurpose, true);
        }

        #region 求助事件
        private void BindSeekHelp()
        {
            //求助
            gvSeekHelp.RecordCount = Convert.ToInt32(SeekHelpDataSource.Tables[1].Rows[0][0]);
            gvSeekHelp.DataSource = SeekHelpDataSource.Tables[0];
            gvSeekHelp.DataBind();
        }

        protected void gvSeekHelp_PreDataBound(object sender, EventArgs e)
        {
            // 数据绑定之前，进行权限检查
            //CheckPowerWithWindowField("DonationEdit", gvDonation, "editField");
            //CheckPowerWithLinkButtonField("DonationDelete", gvDonation, "deleteField");
        }

        protected void gvSeekHelp_Sort(object sender, GridSortEventArgs e)
        {
            gvSeekHelp.SortDirection = e.SortDirection;
            gvSeekHelp.SortField = e.SortField;
            BindSeekHelp();
        }

        protected void ddlSeekHelpPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            gvDonation.PageSize = Convert.ToInt32(ddlSeekHelpPageSize.SelectedValue);
            BindSeekHelp();
        }

        protected void gvSeekHelp_PageIndexChange(object sender, GridPageEventArgs e)
        {
            gvSeekHelp.PageIndex = e.NewPageIndex;
            BindDonation();
        }

        protected void gvSeekHelp_RowCommand(object sender, GridCommandEventArgs e)
        {
            string id = GetSelectedDataKeyID(gvDonation);

            if (e.CommandName == "Delete")
            {
                // 在操作之前进行权限检查
                if (!CheckPower("DonationDelete"))
                {
                    CheckPowerFailWithAlert();
                    return;
                }

                var del = DB.PartyDonations.FirstOrDefault(t => t.ID == id);
                DB.PartyDonations.Remove(del);
                DB.SaveChanges();
                BindDonation();
            }
        }
        #endregion

        #region 捐助事件
        private void BindDonation()
        {
            //捐助
            gvDonation.RecordCount = Convert.ToInt32(DonationDataSource.Tables[1].Rows[0][0]);
            gvDonation.DataSource = DonationDataSource.Tables[0];
            gvDonation.DataBind();
        }

        protected void gvDonation_PreDataBound(object sender, EventArgs e)
        {
            // 数据绑定之前，进行权限检查
            //CheckPowerWithWindowField("DonationEdit", gvDonation, "editField");
            //CheckPowerWithLinkButtonField("DonationDelete", gvDonation, "deleteField");
        }

        protected void gvDonation_Sort(object sender, GridSortEventArgs e)
        {
            gvDonation.SortDirection = e.SortDirection;
            gvDonation.SortField = e.SortField;
            BindDonation();
        }

        protected void ddlDonationPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            gvDonation.PageSize = Convert.ToInt32(ddlDonationPageSize.SelectedValue);
            BindDonation();
        }

        protected void gvDonation_PageIndexChange(object sender, GridPageEventArgs e)
        {
            gvDonation.PageIndex = e.NewPageIndex;
            BindDonation();
        }

        protected void gvDonation_RowCommand(object sender, GridCommandEventArgs e)
        {
            string id = GetSelectedDataKeyID(gvDonation);

            if (e.CommandName == "Delete")
            {
                // 在操作之前进行权限检查
                if (!CheckPower("DonationDelete"))
                {
                    CheckPowerFailWithAlert();
                    return;
                }

                var del = DB.PartyDonations.FirstOrDefault(t => t.ID == id);
                DB.PartyDonations.Remove(del);
                DB.SaveChanges();
                BindDonation();
            }
        }
        #endregion

        #endregion

        #region 按钮事件
        /// <summary>
        /// 查询按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindSeekHelp();
            BindDonation();
        }

        /// <summary>
        /// 匹配按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMate_Click(object sender, EventArgs e)
        {
            #region 在操作之前进行权限检查
            if (!CheckPower("DonAndSeekMate"))
            {
                CheckPowerFailWithAlert();
                return;
            }
            #endregion

            // 从每个选中的行中获取ID（在gvSeekHelp中定义的DataKeyNames）
            List<string> sh_ids = GetSelectedDataKeyIDs(gvSeekHelp);//求助
            List<string> do_ids = GetSelectedDataKeyIDs(gvDonation);//捐助 

            #region 验证
            if (sh_ids.Count() == 0 || do_ids.Count() == 0)
            {
                Alert.Show("请选择一条求助信息和一条捐助信息！", MessageBoxIcon.Warning);
                return;
            }
            if (sh_ids.Count() > 1)
            {
                Alert.Show("求助信息只能选择一条！", MessageBoxIcon.Warning);
                return;
            }
            if (do_ids.Count() > 1)
            {
                Alert.Show("捐助信息只能选择一条！", MessageBoxIcon.Warning);
                return;
            }
            #endregion

            string openUrl = string.Format("DonAndSeekEdit.aspx?shid={0}&doid={1}", sh_ids[0], do_ids[0]);
            PageContext.RegisterStartupScript(Dialog.GetShowReference(openUrl,"捐助和求助匹配"));
        }
        #endregion

        protected void Dialog_Close(object sender, EventArgs e)
        {
            BindSeekHelp();
            BindDonation();
        }
    }
}